package com.sdy.healthcode.h5.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.healthcode.biz.dto.CarRecordDTO;
import com.sdy.healthcode.biz.model.HsDepartment;
import com.sdy.healthcode.biz.model.User;
import com.sdy.healthcode.biz.model.VehicleRegistration;
import com.sdy.healthcode.biz.service.HsDepartmentService;
import com.sdy.healthcode.biz.service.UserService;
import com.sdy.healthcode.biz.service.VehicleRegistrationService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/cardRecord")
@Api(tags = "车辆登记")
public class CarRecordController extends BaseController {
    @Autowired
    private VehicleRegistrationService vehicleRegistrationService;
    @Autowired
    private UserService userService;
    @Autowired
    private HsDepartmentService hsDepartmentService;

    /**
     * 外来车辆登记
     */
    @PostMapping("/carRecord")
    @ApiOperation(value = "外来车辆登记", response = CarRecordDTO.class)
    public Response carRecord(HttpServletRequest request, @RequestBody @ApiParam(name = "carRecordDTO",
            value = "实体类") CarRecordDTO carRecordDTO) throws BizException {
        Integer userId = getUserId(request);
        CarRecordDTO ve = carRecordDTO.setUserId(userId);
        Boolean carRecord = vehicleRegistrationService.createCarRecord(ve);
        return Response.success(carRecord);
    }

    @GetMapping("/getUserCarInfo")
    @ApiOperation(value = "车辆登记前获取用户信息", response = User.class)
    public Response<User> getUserCarInfo(HttpServletRequest request) {
        Integer userId = getUserId(request);
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getId, userId).isNull(User::getCardId);
        User one = userService.getOne(userWrapper);
        if (null == one) {
            User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getId, userId));
            return Response.success(user);
        }
        User user = one.setPeopleType(1);
        return Response.success(user);
    }

    @GetMapping("/getUserCarRegisterInfo")
    @ApiOperation(value = "获取前期车辆登记过的数据", response = User.class)
    public Response<CarRecordDTO> getUserCarRegisterInfo(HttpServletRequest request) {
        Integer userId = getUserId(request);
        User one = userService.getById(userId);
        List<VehicleRegistration> list = vehicleRegistrationService.list(Wrappers.<VehicleRegistration>lambdaQuery()
                .eq(VehicleRegistration::getUserId, userId).orderByDesc(VehicleRegistration::getCreateTime));
        CarRecordDTO carRecordDTO = new CarRecordDTO();
        if (list.isEmpty()) {
            carRecordDTO
                    .setUserId(one.getId())
                    .setPhone(one.getPhone())
                    .setName(one.getName())
                    .setCardId(one.getCardId())
                    .setCarNum(one.getCarNum());
            return Response.success(carRecordDTO);
        }

        // 获取最新的一条登记数据
        VehicleRegistration vehicleRegistration = list.get(0);
        // 拿出部门信息
        String deptName = null;
        if (!list.isEmpty() && list.get(0).getVisitDepartment() != null) {
            HsDepartment byId = hsDepartmentService.getById(list.get(0).getVisitDepartment());
            deptName = byId.getName();
        }

        carRecordDTO
                .setUserId(one.getId())
                .setPhone(one.getPhone())
                .setName(one.getName())
                .setCardId(one.getCardId())
                .setAction(vehicleRegistration.getAction())
                .setCarNum(vehicleRegistration.getCarNum())
                .setVisitDeptId(vehicleRegistration.getVisitDepartment())
                .setVisitPeople(vehicleRegistration.getVisitPeople())
                .setVisitDeptStr(deptName);
        return Response.success(carRecordDTO);
    }

}
